﻿using System;
using System.Collections.Generic;
using System.IO;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKCoordinate;

namespace SetPipe.Utiltiy
{
	public class FamilyCreater
	{
		private FamilyCreater()
		{
		}

		public static FamilyCreater GetFamilyCreater()
		{
			return FamilyCreater.singleTag;
		}

		public string SetResultData(UIApplication ui_app, PipeBoundryCurvePakcer input, double deep)
		{
			this.reSet();
			this.ui_app = ui_app;
			this.deep = deep;
			this.boundryCurvePacker = input;
			this.creatTransform();
			this.creatFamily();
			return this.outPutPath;
		}

		private void reSet()
		{
			this.ui_app = null;
			this.deep = 0.0;
			this.transFormdriection = null;
			this.startPoint = null;
			this.endPoint = null;
			if (this.lstBoundryCurve == null)
			{
				this.lstBoundryCurve = new List<Curve>();
			}
			else
			{
				this.lstBoundryCurve.Clear();
			}
			this.outPutPath = null;
		}

		public XYZ GetRealStartPoint()
		{
			return this.boundryCurvePacker.GetPoint(0);
		}

		private void creatTransform()
		{
			this.startPoint = XYZ.Zero;
			this.transFormdriection = this.startPoint.Add(-this.boundryCurvePacker.GetPoint(0));
			this.transForm = Transform.CreateTranslation(this.transFormdriection);
			this.endPoint = this.boundryCurvePacker.GetPoint(1).Add(this.transFormdriection);
			foreach (Curve curve in this.boundryCurvePacker.LstBoundryCurves)
			{
				this.lstBoundryCurve.Add(curve.CreateTransformed(this.transForm));
			}
		}

		public void creatFamily()
		{
			if (!File.Exists(StaticRescource.StrPathFamilyTemplate))
			{
				this.outPutPath = null;
				return;
			}
			Document document = this.ui_app.Application.NewFamilyDocument(StaticRescource.StrPathFamilyTemplate);
			if (!document.IsFamilyDocument)
			{
				this.outPutPath = null;
				return;
			}
			FamilyManager familyManager = document.FamilyManager;
			Application application = document.Application;
			Transaction transaction = new Transaction(document, "creatFamily");
			transaction.Start();
			CurveArrArray curveArrArray = application.Create.NewCurveArrArray();
			CurveArray curveArray = application.Create.NewCurveArray();
			foreach (Curve curve in this.lstBoundryCurve)
			{
				curveArray.Append(curve);
			}
			curveArrArray.Append(curveArray);
			Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero);
			SketchPlane sketchPlane = SketchPlane.Create(document, plane);
			document.FamilyCreate.NewExtrusion(true, curveArrArray, sketchPlane, -this.deep);
			transaction.Commit();
			SaveAsOptions saveAsOptions = new SaveAsOptions();
			saveAsOptions.OverwriteExistingFile = true;
			this.outPutPath = StaticRescource.StrPathOutPut + StaticRescource.GetADateTime() + this.outPutInex.ToString() + ".rfa";
			document.SaveAs(this.outPutPath, saveAsOptions);
			document.Close();
			this.outPutInex++;
		}

		private UIApplication ui_app;

		private double deep;

		private PipeBoundryCurvePakcer boundryCurvePacker;

		private XYZ transFormdriection;

		private Transform transForm;

		private XYZ startPoint;

		private XYZ endPoint;

		private List<Curve> lstBoundryCurve;

		private int outPutInex;

		private string outPutPath;

		private static FamilyCreater singleTag = new FamilyCreater();
	}
}
